﻿using System;
using System.Collections.Generic;
using System.Text;
using Antu.WebControls;
using EluZSProvider;
using Antu.Core.Services;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZhengShouZBBPersonEditControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        Button btn_Update, btn_fanhui;
        DropDownList txt_xzry, txt_atxzry;
        TextBox txt_xzrydh, txt_atxzrydh, txtbz;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            //if (HttpContext.Current.Session["UserID"] == null)
            //{
            //    Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            //}
            //else
            //{
            //    userid = HttpContext.Current.Session["UserID"].ToString();
            //}
            
            base.OnLoad(e);
            InitControl();
            if (!Page.IsPostBack)
            {
                InitBind();
                Bind();
            }
            
        }

        protected void InitControl()
        {
            txtbz = (TextBox)FindControl("txtbz");
            txt_xzry = (DropDownList)FindControl("txt_xzry");
            txt_xzry.SelectedIndexChanged += new EventHandler(txt_xzry_SelectedIndexChanged);
            txt_xzrydh = (TextBox)FindControl("txt_xzrydh");
            txt_atxzry = (DropDownList)FindControl("txt_atxzry");
            txt_atxzry.SelectedIndexChanged += new EventHandler(txt_atxzry_SelectedIndexChanged);
            txt_atxzrydh = (TextBox)FindControl("txt_atxzrydh");
            btn_Update = (Button)FindControl("btn_Update");
            btn_Update.Click += new EventHandler(btn_Update_Click);
            btn_fanhui = (Button)FindControl("btn_fanhui");
            btn_fanhui.Click += new EventHandler(btn_fanhui_Click);
        }

        protected void txt_xzry_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txt_atxzry.SelectedValue != "请选择")
            {
                string sql = "select keyvalue from zs_params where keyname = '" + txt_xzry.SelectedValue + "' ";
                DataTable dt = _EluZSDataProvider.GetTable(sql);
                txt_xzrydh.Text = "";
                txt_xzrydh.Text = dt.Rows[0]["keyvalue"].ToString();
            }
        }

        protected void txt_atxzry_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txt_atxzry.SelectedValue!="请选择")
            {
                string sql = "select keyvalue from zs_params where keyname = '" + txt_atxzry.SelectedValue + "' ";
                DataTable dt = _EluZSDataProvider.GetTable(sql);
                txt_atxzrydh.Text = "";
                txt_atxzrydh.Text = dt.Rows[0]["keyvalue"].ToString();
            }
           
        }

     

        protected void InitBind()
        {
            string sql = "select keyname,keyvalue from zs_params where name = '小组人员'";
            txt_xzry.DataSource = _EluZSDataProvider.GetTable(sql);
            txt_xzry.DataTextField = "keyname";
            txt_xzry.DataValueField = "keyname";
            txt_xzry.DataBind();
            txt_xzry.Items.Insert(0, "请选择");
            string sql1 = "select keyname,keyvalue from zs_params where name = '安图人员'";
            txt_atxzry.DataSource = _EluZSDataProvider.GetTable(sql1);
            txt_atxzry.DataTextField = "keyname";
            txt_atxzry.DataValueField = "keyname";
            txt_atxzry.DataBind();
            txt_atxzry.Items.Insert(0, "请选择");
        }

        protected void Bind()
        {
            string sql = string.Empty;
            sql = "Select * from zs_zrb_person where riqi = '" + Page.Request["type"].ToString() + "' and  zid ='" + Page.Request["zId"].ToString() + "'";
          
            DataTable dt =  _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0 && dt.Rows[0]["name"].ToString() != null && dt.Rows[0]["name"].ToString() != "")
            {
                txt_xzry.Text = dt.Rows[0]["name"].ToString();
                txt_xzrydh.Text = dt.Rows[0]["xznumber"].ToString();
                txt_atxzry.Text = dt.Rows[0]["atname"].ToString();
                txt_atxzrydh.Text = dt.Rows[0]["atnumber"].ToString();
                txtbz.Text = dt.Rows[0]["备注"].ToString();
            }
            if (Page.Request["zid"]!=null)
            {
                string sql1 = "Select name,atname,xznumber,atnumber,备注 from zs_zrb_person where zid = '" + Page.Request["zid"] + "' and 序列 = '" + Page.Request["type"] + "'";
                DataTable dt1 = _EluZSDataProvider.GetTable(sql1);
                txt_xzry.SelectedValue = dt1.Rows[0]["name"].ToString();
                txt_atxzry.SelectedValue = dt1.Rows[0]["atname"].ToString();
                txt_xzrydh.Text = dt1.Rows[0]["xznumber"].ToString();
                txt_atxzrydh.Text = dt1.Rows[0]["atnumber"].ToString();
                txtbz.Text = dt1.Rows[0]["备注"].ToString();
            }
        }

        protected void btn_Update_Click(object sender, EventArgs e)
        {
            string sql = "update zs_zrb_person set name='" + txt_xzry.Text + "',atname='" + txt_atxzry.Text + "',atnumber='" + txt_atxzrydh.Text + "',xznumber='" + txt_xzrydh.Text + "',备注='" + txtbz.Text + "' where zid ='" + Page.Request["zId"].ToString() + "' and 序列 = '" + Page.Request["type"].ToString() + "'";

            _EluZSDataProvider.RunSqlNon(sql);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "s2", "<script>alert('保存成功!');location.href ='ZhengShouZBBPerson.aspx?id=" + Page.Request["ZId"].ToString()+"';</script>");
        }


        protected void btn_fanhui_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect("zhengshouzbbperson.aspx?id=" + Page.Request["ZId"].ToString() + "");
        }

    }
}
